from lib import common
from interface import shopping_interface
from conf import setting

#1.商家查看商品列表
def check_goods():
    print('查看商品列表页面')
    goods_list=[]
    while True:
        flag,msg=shopping_interface.check_goods_interface(setting.GOODS_PATH)
        if flag:
            for line in msg:
                goods_list.append(line.replace('.json',''))
            for n,good in enumerate(goods_list):
                print('%s.%s'%(n,good))
            print('-'*100)
            break
        else:
            print(msg)
            break


#2.上架商品
def add_goods():
    print('添加商品页面')
    while True:
        name=input('请输入添加的商品名称:').strip()
        if name.isdigit():continue
        price=input('请输入商品的价格:').strip()
        if price.isdigit():
            price=float(price)
            flag,msg=shopping_interface.add_goods_interface(name,price)
            if flag:
                print(msg)
                break
            else:
                print(msg)
                break

        else:
            print('商品价格只能为数字..')
            break



#3.下架商品
def remove_goods():
    print('下架商品页面')
    while True:
        name=input('请输入要下架商品的名称:').strip()
        conf_name=input('请再次确认要下架商品的名称:').strip()
        if name==conf_name:
            flag,msg=shopping_interface.remove_goods_interface(name)
            if flag:
                print(msg)
                break
            else:
                print(msg)
                break
        else:
            print('两次输入的商品名称不正确，请核对后重新输入')
            break


#4.更新商品
def update_goods():
    print('更新商品页面<价格，库存>')
    while True:
        name=input('请输入要更新的商品的名称:').strip()
        price=input('请输入要修改的价格:').strip()
        count=input('请输入要修改的库存数量:').strip()
        if not price.isdigit() or not count.isdigit():
            print('价格或数量只能为数字')
            break
        flag,msg=shopping_interface.update_goods_interface(name,price,count)
        if flag:
            print(msg)
            break
        else:
            print(msg)
            break











